package com.zbkj.admin.task.assistant;


import cn.hutool.core.util.ObjUtil;
import com.zbkj.admin.controller.platform.assistant.service.AssistantConversationService;
import com.zbkj.admin.controller.platform.assistant.service.AssistantMessageService;
import com.zbkj.admin.controller.platform.assistant.service.AssistantSettingService;
import com.zbkj.admin.task.order.OrderReceiptTask;
import com.zbkj.common.model.assistant.AssistantConversation;
import com.zbkj.common.model.assistant.AssistantSetting;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.service.service.UserBrokerageRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 客服会话排队处理task
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@Component("AssistantConversationTask")
@RequiredArgsConstructor
public class AssistantConversationTask {

    private final AssistantConversationService assistantConversationService;
    private final AssistantSettingService assistantSettingService;
    private final AssistantMessageService assistantMessageService;

    /**
     * 处理客服会话排队队列
     */
    public void processQueue() {
        // cron : 0 */1 * * * ?
        try {
            AssistantSetting setting = assistantSettingService.getSetting();
            List<AssistantConversation> queueList = assistantConversationService.findQueueList();
            if (ObjUtil.isEmpty(queueList)) return;

            // 有排队的人，需要进行分配
            for (AssistantConversation conversation : queueList) {
                Boolean isToService = assistantConversationService.processQueue(setting, conversation);
                if (!isToService) {
                    break;
                }
            }

        } catch (Exception e) {
            log.error("处理客服会话排队队列任务异常 ", e);
        }
    }

    /**
     * 处理会话超时
     */
    public void processConversationTimeout() {
        // cron : 0 */1 * * * ?
        try {
            AssistantSetting setting = assistantSettingService.getSetting();
            if (!setting.getIsConversationTimeoutAutoClose()) {
                return;
            }

            assistantConversationService.findServiceList()
                .stream()
                .filter(x -> assistantMessageService.checkIsTimeout(setting, x))
                .forEach(x -> assistantConversationService.timeout(setting, x));

        } catch (Exception e) {
            log.error("处理客服会话排队队列任务异常 ", e);
        }
    }
}
